var spceType = getUrlParam('spaceType');
var teamId = getUrlParam('teamId');
var currrentForldId = getUrlParam('forldId');
var userId = getUrlParam('user_id');
var selectId = 0;
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
function init() {
    //目录树结构
    $.ajax({
        type: 'get',
        dataType: 'json',
        url: 'TreeForld.ashx',
        data: { forldId: currrentForldId, spaceType: spceType, teamID: teamId, userId: userId },
        cache: false,
        success: function (_data) {
            var _dataarr = [];
            $.each(_data, function (i, item) {
                var obj = {};
                obj.id = item.ObjectID;
                obj.text = item.ObjectName;
                obj.parentId = item.PObjectID;
                _dataarr.push(obj);
            })
            $('#filetree').tree({
                data: _dataarr,
                onClick: function (node) {
                    var selected = $('#filetree').tree('getSelected');
                    selectId = selected.id;

                    loadbindgrid(selected);

                }
            });
        }
    })


    //分组类别事件
    $('#sortlist ul li').bind('click', function () {
        $(this).siblings().removeClass('current');
        $(this).addClass('current');

        if ($('#dhtree a').length > 0) {
            $('#dhtree a:last').click();
        }
    });
    loadbindgrid(null);
}

//function loadbindgrid1(id) {
//    var arr = []; var dharr = [];

//    dharr.unshift('<a href="javascript:void(0)" onclick="gototree(0)">..</a>');
//   
//    $('#dhtree').html(dharr.join('<span class="splittag">></span>'));
//  
//    //查询分组统计
//    groupcount(id);
//}


function loadbindgrid(selected) {
    var arr = []; var dharr = [];
    if (selectId != 0) {
        arr = getdh(selectId);

        $.each(arr, function (i, item) {
            dharr.unshift('<a href="javascript:void(0)" onclick="gototree(' + item.id + ')">' + item.text + '</a>');
        });
        dharr.unshift('<a href="javascript:void(0)" onclick="gototree(0)">..</a>');

        $('#dhtree').html(dharr.join('<span class="splittag">></span>'));
        if ($('#filetree').tree('getChildren', selected.target).length == 0) {
            bindsubtree(selected);

        } else {
            //子目标
            var subarr = getfstnode(selectId);
            if (subarr && subarr.length > 0) {
                var _htmlarr = [];
                $.each(subarr, function (i, item) {
                    _htmlarr.push('<div><a href="javascript:void(0)" onclick="gototree(' + item.id + ')">' + item.text + '</a></div>');
                });
                $('#subfiletree').html(_htmlarr.join(''));
            }
        }
    }
    else {
        dharr.unshift('<a href="javascript:void(0)" onclick="gototree(0)">..</a>');
        $('#dhtree').html(dharr.join('<span class="splittag">></span>'));
        $('#subfiletree').html('');
    }
    
    //查询分组统计
    groupcount(selectId);
}

//#region 子树
function bindsubtree(selected) {
    $.ajax({
        type: 'get',
        dataType: 'json',
        url: 'TreeForld.ashx',
        data: { forldId: selectId, spaceType: spceType, teamID: teamId, userId: userId },
        cache: false,
        success: function (_data) {
            if (_data && _data.length > 0) {
                var _dataarr = [];
                $.each(_data, function (i, item) {
                    var obj = {};
                    obj.id = item.ObjectID;
                    obj.text = item.ObjectName;
                    obj.parentId = item.PObjectID;
                    _dataarr.push(obj);
                });
                $('#filetree').tree('append', {
                    parent: selected.target,
                    data: _dataarr
                });
                //子目标
                var subarr = getfstnode(selectId);
                if (subarr && subarr.length > 0) {
                    var _htmlarr = [];
                    $.each(subarr, function (i, item) {
                        _htmlarr.push('<div><a href="javascript:void(0)" onclick="gototree(' + item.id + ')">' + item.text + '</a></div>');
                    });
                    $('#subfiletree').html(_htmlarr.join(''));
                }
            } //if
            else {
                $('#subfiletree').html('');
            }
        }


    });
}
//#endregion

//#region 
function getdh(id) {
    var current = $('#filetree').tree('find', id);
    var arr = [];
    arr.push(current);
    getsubdh(current, arr);
    function getsubdh(selected, arr) {
        var _parent = $('#filetree').tree('getParent', selected.target);
        if (_parent) {
            arr.push(_parent);
            getsubdh(_parent, arr);
        }
    }
    return arr;
}

//子节点
function getfstnode(id) {
    var current = $('#filetree').tree('find', id);
    var nodes = [];
    $(current.target).next().children().children("div.tree-node").each(function () {
        nodes.push($('#filetree').tree('getNode', this));
    });
    return nodes;
}
//#endregion

//#region tree跳转
function gototree(id) {
    if (id != 0) {
        var selected = $('#filetree').tree('find', id);
        selectId = selected.id;
        loadbindgrid(selected);
    }
    else {
        selectId = 0;
        loadbindgrid(selectId);
    }
}
//#endregion

//#region 查询分组统计
function groupcount(id) {
    $.ajax({
        type: 'get',
        dataType: 'json',
        url: 'ClassFileHandler.aspx',
        data: { forldId: id, spaceType: spceType, teamID: teamId, userId: userId, classType: $('#sortlist ul li.current').attr('_value') },
        cache: false,
        success: function (_data) {
            if (_data && _data.length > 0) {
                $.each(_data, function (i, item) {
                    _data[i].id = i + 1;
                    _data[i].groupName = _data[i].groupName + '(' + _data[i].number + ')';
                });
                bindtreegrid(_data);
            }
            else {
                bindtreegrid(null);
            }
        }
    });
}


function bindtreegrid(_data) {
    $('.panel.datagrid').html('<div id="tablegrid"></div>');

    //    if (g_node_id != null) {
    //        $('#tablegrid').treegrid('remove', g_node_id);
    //        g_node_id = null;

    //    }
    //treegrid树
    $('#tablegrid').treegrid({
        idField: 'id',
        treeField: 'groupName',

        columns: [[
	        { title: '', field: 'groupName', width: '100%', align: 'left', formatter: function (v, row, index) {
	            if (row.number) {
	                return row.groupName;
	            } else if ($('#sortlist li.current').attr('_value') == "DEST_OBJ" || $('#sortlist li.current').attr('_value') == "FILE_SIZE" || $('#sortlist li.current').attr('_value') == "FILE_EXT" || $('#sortlist li.current').attr('_value') == "BIZ_OBJ" || $('#sortlist li.current').attr('_value') == "CREATE_TIME") {
	                return '<div class="row-box"><div class="row-box-left"><div>' + row.FILE_NAME + '</div><div class="fileattr"><span>' + row.FILE_SIZE + '</span><span style="padding-left:20px;">' + row.CREATE_TIME + '</span></div></div></div><div class="buttons" style="width: 150px;margin-top: 7px; display: block;float:right"><input onclick="edit(' + row.ID + ')" type="button" style="width:25px" class="editable"><input onclick="browse(' + row.ID + ')" type="button" style="width:25px" class="visible"><input onclick="download(' + row.ID + ')" type="button" style="width:25px" class="download"></div>'
	            }
	        }
	        }
	    ]],
        data: {
            "rows": _data
        }
	, onClickRow: function (row) {
	    var node = $('#tablegrid').treegrid('getSelected');
	    var selected = $('#filetree').tree('getSelected');
	    if ($('#tablegrid').treegrid('getChildren', node.id).length == 0 && !$('#tablegrid').treegrid('getParent', node.id)) {
	        $.ajax({
	            type: 'get',
	            dataType: 'json',
	            url: 'DetailHandler.ashx',
	            data: { forldId: selectId, spaceType: spceType, teamID: teamId, userId: userId, classType: $('#sortlist ul li.current').attr('_value'), groupItem: node.groupItem },
	            cache: false,
	            success: function (d) {
	                g_node_id = node.id;
	                $('#tablegrid').treegrid('append', {
	                    parent: node.id,
	                    data: d
	                });
	                $('.row-box').parent().parent().css('height', '42px');
	                $('#tablegrid').treegrid('expandAll', node.id);

	            }
	        });
	    } else {

	        $('#tablegrid').treegrid('toggle', node.id);
	    }

	}
    });
}
//#endregion  